package com.wang.web3;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.wang.web3.base.Constant;
import com.wang.web3.bean.ContractBean;
import com.wang.web3.bean.GirlBean;
import com.wang.web3.bean.TianWallet;
import com.wang.web3.contracts.GirlCore;
import com.wang.web3.utils.SPUtils;
import com.wang.web3.weight.GirlRepoWindow;

import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.Web3jFactory;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.http.HttpService;

import java.math.BigInteger;
import java.util.List;
import java.util.concurrent.ExecutionException;

import rx.functions.Action1;


public class MyGirlsDetailActivity extends AppCompatActivity {


    private TextView tv_prop, tv_stronger, tv_call, tv_buyback, tv_resolved;
    private ImageView iv_img;
    private TextView tv_girl_name, tv_story;
    private String girl_img;
    private int girl_id;
    private GirlBean.DataBean dataBean;
    boolean isOpen;

    Gson gson = new Gson();
    GirlCore girlCore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_girls_detail);
        initData();
        initView();
    }

    private void initData() {
        Intent i = getIntent();
        girl_img = i.getStringExtra("girl_img");
        girl_id = i.getIntExtra("girl_id", 0);
        dataBean = (GirlBean.DataBean) i.getSerializableExtra("girlB");

        Web3j web3 = Web3jFactory.build(new HttpService(Constant.URL));
        String wallet = (String) SPUtils.get(this,Constant.WALLETLOCKADDRESS,"");
        String contract = (String) SPUtils.get(this,Constant.CONTRACTADDRESS,"");
        TianWallet tianWallet = gson.fromJson(wallet, TianWallet.class);
        ContractBean contractBean = gson.fromJson(contract, ContractBean.class);
        Credentials credentials = Credentials.create(tianWallet.getPrivateKey().substring(2));
        if (contractBean!=null && contractBean.getMagicKeys() != null ){
            girlCore = GirlCore.load(contractBean.getGirlCore(),web3,credentials,Constant.GAS_PRICE,Constant.GAS_LIMIT);
        }
    }

    private void initView() {
        tv_prop = findViewById(R.id.tv_prop);
        tv_stronger = findViewById(R.id.tv_stronger);
        tv_call = findViewById(R.id.tv_call);
        tv_buyback = findViewById(R.id.tv_buyback);
        tv_resolved = findViewById(R.id.tv_resolved);
        iv_img = findViewById(R.id.iv_img);
        tv_girl_name = findViewById(R.id.tv_girl_name);
        tv_story = findViewById(R.id.tv_story);
        Glide.with(this).load(girl_img).into(iv_img);
        tv_girl_name.setText("少女"+girl_id);
        findViewById(R.id.tv_stronger).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyGirlsDetailActivity.this, GirlStrongActivity.class));
            }
        });
        findViewById(R.id.tv_call).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyGirlsDetailActivity.this, GirlSummonActivity.class));
            }
        });
        findViewById(R.id.tv_resolved).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyGirlsDetailActivity.this, GirlBreakDownActivity.class));
            }
        });

        findViewById(R.id.tv_buyback).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final GirlRepoWindow girlRepoWindow = GirlRepoWindow.showKitty(getSupportFragmentManager(),dataBean);
                girlRepoWindow.initOnClick(new GirlRepoWindow.KittyWindowOnClick() {
                    @Override
                    public void onClick(View v) {
                        if (v.getId() == R.id.mBtnRepo){
                            if (isOpen){
                                return;
                            }
                            isOpen = true;
                            Toast.makeText(MyGirlsDetailActivity.this,"正在回购",Toast.LENGTH_SHORT).show();
                            final RemoteCall<TransactionReceipt> rm = girlCore.recycleGirl(new BigInteger("" + dataBean.getId()));
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    rm.observable().subscribe(new Action1<TransactionReceipt>() {
                                        @Override
                                        public void call(TransactionReceipt transactionReceipt) {
                                            List<GirlCore.RecycleGirlEventResponse> list = girlCore.getRecycleGirlEvents(transactionReceipt);
                                            Log.e("Tag","------");
                                            isOpen = false;
                                            girlRepoWindow.dismiss();
                                        }
                                    });
                                }
                            }).start();
                        }else{
                            girlRepoWindow.dismiss();
                        }
                    }
                });
            }
        });
    }

}